package com.itheima04;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.itheima04.utils.JDBCUtils;

/**
 * @Author TDD
 * @Date 2021/6/22 20:01
 * @Version 1.0
 */
public class Transfer {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement pstat1 = null ;
        PreparedStatement pstat2 = null ;

        //jack给rose转账1000元
        try {
            con = JDBCUtils.getConnection();
            con.setAutoCommit(false);
            String sql1 = "update account set money = money - 1000 where name = 'jack'";
            String sql2 = "update account set money = money + 1000 where name = 'rose'";

            pstat1 = con.prepareStatement(sql1);
            pstat2= con.prepareStatement(sql2);

            int i1 = pstat1.executeUpdate();
            //int i  = 10 / 0 ;
            int i2 = pstat2.executeUpdate();

            if(i1>0 && i2>0){
                con.commit();
            }else {
                con.rollback();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con,pstat1,pstat2);
        }
    }
}
